<?php

class DatamemberController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $session = new Zend_Session_Namespace('Zend_Auth_Admin');
        $this->sess = $session->data;

        $this->_helper->layout->setLayout('datamember');

        if(!isset($this->sess)){
            $param = array(
            'action' => 'admin',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        $session->action = $this->param['action'];

       

    }

    public function indexAction()
    {
        //
    }

    public function editAction()
    {


        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            //Member Detail
            $memberId = $formData['member_id'];
            $memberCode = $formData['member_code'];
            $memberName = $formData['member_name'];

            
            //Member Address
            $addrId = $formData['contact_address_id'];
            $addr1 = $formData['address_line1'];
            $addr2 = $formData['address_line2'];
            $addrCity = $formData['city'];
            $addrRegion = $formData['region'];
            $addrPostcode  = $formData['postal_code'];
            $addrCountry  = $formData['country'];

            //Member Contact
            $contId = $formData['member_contact_id'];
            $contName = $formData['contact_name'];
            $contMobile = $formData['mobile_no'];
            $contPhone = $formData['phone'];
            $contEmail = $formData['email'];
            $contKtp = $formData['no_ktp'];

            //Ahli Waris
            $warisId = $formData['member_contact_idw'];
            $warisName = $formData['waris_name'];
            $warisMobile = $formData['waris_mobile_no'];
            $warisPhone = $formData['waris_phone'];
            $warisEmail = $formData['waris_email'];
            $warisKtp = $formData['waris_ktp'];

            //Bank Acc
            $accBankName = $formData['bank_name'];
            $accAccountName = $formData['account_name'];
            $accBankNo = $formData['bank_account_no'];
            $accBankCity = $formData['bank_city'];

            $errorMsg = "";


            $modelMember = new Application_Model_Member();

            /*
             * Check HP + KTP + Rekening
             */
            $cekMember = $modelMember->isExistMember($contMobile, $contKtp, $accBankNo, $memberId, $memberCode);
            if ($cekMember["hp"] > 0){
                $errorMsg .= "* Nomor HP sudah terdaftar.<br>";
            }

            if ($cekMember["ktp"] > 0){
                $errorMsg .= "* Nomor KTP sudah terdaftar.<br>";
            }

            if ($cekMember["rek"] > 0){
                $errorMsg .= "* Nomor Rekening sudah terdaftar.<br>";
            }


            if ($errorMsg == ""){

                //Member
                $dataMember = array(
                        'member_name' => $memberName,                                                
                        );

                $memberWhere = array(
                    'member_id = ?' => $memberId
                );


                //Address
                $dataAddress = array(
                        'addr1' => $addr1,
                        'addr2' => $addr2,
                        'addr3' => $addrCity,
                        'addr4' => $addrRegion,
                        'addr5' => $addrPostcode,
                        'country' => $addrCountry
                        );

                $addressWhere = array(
                    'contact_address_id = ?' => $addrId
                );

                //Contact
                $dataContact = array(
                        'contact_name' => $contName,
                        'mobile_no' => $contMobile,
                        'phone' => $contPhone,
                        'email' => $contEmail,
                        'no_ktp'  => $contKtp
                        );

                $contactWhere = array(
                    'member_contact_id = ?' => $contId
                );


                //Ahli waris
                $dataAhliWaris = array(
                        'contact_name' => $warisName,
                        'mobile_no' => $warisMobile,
                        'phone' => $warisPhone,
                        'email' => $warisEmail,
                        'no_ktp'  => $warisKtp
                        );

                $warisWhere = array();
                if ($warisId != ""){
                    $warisWhere = array(
                        'member_contact_id = ?' => $warisId
                    );
                }


                //Account
                $dataAccount = array(
                        'bank_name' => $accBankName,
                        'account_name' => $accAccountName,
                        'bank_account_no' => $accBankNo,
                        'bank_city' => $accBankCity
                        );

                $accountWhere = array(
                    'account_num = ?' => $memberCode
                );


                $data = $modelMember->editMember($dataMember, $memberWhere, $dataAccount, $accountWhere, 
                                                $dataContact, $contactWhere, $dataAddress, $addressWhere,
                                                $dataAhliWaris, $warisWhere);

                $code = $data['code'];
                $msg = $data['msg'];

                if($code == 0){
                        $func = new Application_Model_Functions();

                        $this->view->code = $code;
                        $this->view->msg = $msg;


                }else{

                    $this->view->code = 1;
                    $this->view->msg = "ERROR ".$msg;

                }

            }else{
                $this->view->code = 1;
                $this->view->msg = "ERROR ".$errorMsg;


            }


        }else{

            $this->view->code = isset($this->param['code']) ? $this->param['code'] : "";
            $this->view->msg = isset($this->param['msg']) ? $this->param['msg'] : "";

            $countryList = new Application_Model_Functions();
            $this->view->countryList = $countryList->countryList();
          
        }
    
        
    }


    public function perkembanganAction()
    {
        //
    }
    
}

